<?php 
include 'method/win.php';
if(!isset($_GET['page'])){
    $page = 1;
}else{
    $page = $_GET['page'];
}

if(isset($_POST['search-btn'])){    
    $_SESSION['search'] = $_POST['str-search'];
}

if(isset($_SESSION['search'])){
//     找微博
    $ctsql = "SELECT weibo_user.user_tx, weibo_list.* FROM weibo_user, weibo_list WHERE weibo_user.user_name = weibo_list.author and 
weibo_list.relwb_id is null and weibo_list.weibo_content LIKE '%".$_SESSION['search']."%'";
    $ctres = $conn->query($ctsql);
    $ctper_page = 20;
    if(!isset($_GET['ctpage'])){
        $ctpage = 1;
    }else{
        $ctpage = $_GET['ctpage'];
    }
    $cttotal_ly = $ctres->num_rows;
    $cttotal_page = ceil($cttotal_ly/$ctper_page);
    
    $ctsql = "SELECT weibo_user.user_tx, weibo_list.* FROM weibo_user, weibo_list WHERE weibo_user.user_name = weibo_list.author
 and weibo_list.relwb_id is null and weibo_list.weibo_content LIKE '%".$_SESSION['search']."%' limit ".($ctpage-1)*$ctper_page.",".$ctper_page;
    $ctres = $conn->query($ctsql);
    
//     找人
    $cusql = "SELECT * FROM weibo_user WHERE user_name LIKE '%".$_SESSION['search']."%'";
    $cures = $conn->query($cusql);
    $cuper_page = 80;
    if(!isset($_GET['cupage'])){
        $cupage = 1;
    }else{
        $cupage = $_GET['cupage'];
    }
    $cutotal_ly = $cures->num_rows;
    $cutotal_page = ceil($cutotal_ly/$cuper_page);
    
    $cusql = "SELECT * FROM weibo_user WHERE user_name LIKE '%".$_SESSION['search']."%' limit ".($cupage-1)*$cuper_page.",".$cuper_page;
    $cures = $conn->query($cusql);
    
    if(isset($_SESSION['user'])){
        header("location:search_loginafter.php");
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>未登录搜索</title>
    <link rel="stylesheet" href="../lib/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="css/search.css">
    <script src="../lib/jquery/jquery.js" type="text/javascript"></script>
    <script src="../lib/bootstrap/js/bootstrap.js" type="text/javascript"></script>
</head>
<body style=background-image:url("../img/seach-bg.jpg")>
<!--顶部导航条-->
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!--头部logo-->
        <div class="navbar-header">
            <a class="navbar-brand" href="#"><em><img src="../img/logo.jpg" height=30 width=30></em><em>小狗微博</em></a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <!--搜索-->
            <form class="navbar-form navbar-left" role="search" method="post" action="search.php">
                <div class="form-group">
                    <input type="text" class="form-control" style="width: 400px" placeholder="Search" name="str-search">
                </div>
                <button type="sumbit" class="btn btn-default" name="search-btn">搜索</button>
            </form>
            <!--首页、注册登录-->
            <ul class="nav navbar-nav navbar-right">
                <li><a href="index.php">首页</a></li>
                <li><a href="register.php">注册</a></li>
                <li><a href="login.php">登录</a></li>

            </ul>
        </div>
    </div>
</nav>

<!--筛选标签-->
<div class="search-classify clearfix">
    <a href="search.php?page=1" class="choose-3" id="nav-1">微博</a>
    <a href="search.php?page=2" id="nav-2">找人</a>
</div>

<!--搜索页版心-->
<div class="search-warp clearfix">
    <!--找微博版心-->
    <div class="weibo-search choose-2">
    <?php 
    $no = 1;
    if($ctres->num_rows){
        while ($row = $ctres->fetch_assoc()){
        //     微博
            echo "<div class='weibo-box clearfix'>";
        //     头像
            echo "<div class='author-tx'>";
            echo "<img src='../".$row['user_tx']."' style='width: 50px; height:50px; border-radius: 50px'></div>";
        //     微博版心
            echo "<div class='weibo-warp'>";
            echo "<div class='author-name'>".$row['author']."</div>";
            echo "<div class='weibo-time'>".$row['public_time']."</div>";
            echo "<div class='weibo-content'>
                       ".$row['weibo_content']."
                  </div>";
            echo "<div class='weibo-photo'>";
            if($row['weibo_img'] != ''){
                echo "<img src='../".$row['weibo_img']."' style='height: 167px; width: auto'>";
            }
            echo "</div></div>";
            
            $csql = "select * from weibo_comment where weibo_id = ".$row['weibo_id'];
            $cres = $conn->query($csql);
            $kcsql = "select * from weibo_kidcomment where weibo_id = ".$row['weibo_id'];
            $kcres = $conn->query($kcsql);
            $cnum = ($cres->num_rows)+($kcres->num_rows);
            
            $rsql = "select * from weibo_list where relwb_id = ".$row['weibo_id'];
            $rres = $conn->query($rsql);
        //     微博操作
            echo "<div class='weibo-op'>";
            echo "<a href='detail.php?id=".$row['weibo_id']."&page=1'><span>";
            echo "<em class='glyphicon glyphicon-comment'></em>";
            echo "<em>".$cnum."</em></span></a>";
            echo "<a href='detail.php?id=".$row['weibo_id']."&page=2'><span>";
            echo "<em class='glyphicon glyphicon-share-alt'></em>";
            echo "<em>".$rres->num_rows."</em></span></a>";
            echo "<a href='detail.php?id=".$row['weibo_id']."'><span>";
            echo "<em class='glyphicon glyphicon-thumbs-up'></em>";
            echo "<em>".$row['weibo_zan']."</em></span></a>";
            echo "</div></div>";
        }
//         加载更多
        if($cttotal_page!="1"){
            if($ctpage==1){
                echo "<div class='loadmore clearfix'>";
                echo "<a href='search.php?page=1&ctpage=1'>";
                echo "<span>首页</span>";
                echo "</a>";
                echo "<a href='search.php?page=1&ctpage=1'>";
                echo "<span>上一页</span>";
                echo "</a>";
                echo "<a href='search.php?page=1&ctpage=".($ctpage+1)."'>";
                echo "<span>下一页</span>";
                echo "</a>";
                echo "<a href='search.php?page=1&ctpage=".$cttotal_page."'>";
                echo "<span>尾页</span>";
                echo "</a>";
                echo "</div>";
            }else if($ctpage==$cttotal_page){
                echo "<div class='loadmore clearfix'>";
                echo "<a href='search.php?page=1&ctpage=1'>";
                echo "<span>首页</span>";
                echo "</a>";
                echo "<a href='search.php?page=1&ctpage=".($ctpage-1)."'>";
                echo "<span>上一页</span>";
                echo "</a>";
                echo "<a href='search.php?page=1&ctpage=".$cttotal_page."'>";
                echo "<span>下一页</span>";
                echo "</a>";
                echo "<a href='search.php?page=1&ctpage=".$cttotal_page."'>";
                echo "<span>尾页</span>";
                echo "</a>";
                echo "</div>";
            }else {
                echo "<div class='loadmore clearfix'>";
                echo "<a href='search.php?page=1&ctpage=1'>";
                echo "<span>首页</span>";
                echo "</a>";
                echo "<a href='search.php?page=1&ctpage=".($ctpage-1)."'>";
                echo "<span>上一页</span>";
                echo "</a>";
                echo "<a href='search.php?page=1&ctpage=".($ctpage+1)."'>";
                echo "<span>下一页</span>";
                echo "</a>";
                echo "<a href='search.php?page=1&ctpage=".$cttotal_page."'>";
                echo "<span>尾页</span>";
                echo "</a>";
                echo "</div>";
            }
        }
    }else {
        echo "查无相关微博";
    }
    ?>
    </div>

    <!--找人版心-->
    <div class="user-search">
    	<?php 
    	$no = 1;
    	if($cures->num_rows){
        	while ($row = $cures->fetch_assoc()){
    //     	用户
        	echo "<div class='user-box clearfix'>";
    //     	用户头像
        	echo "<div class='user-tx'>";
        	echo "<img src='../".$row['user_tx']."' style='width: 90px; height: 90px; border-radius: 50%'>";
        	echo "</div>";
    //     	用户关注条
        	echo "<div class='user-op'><a href='person.php?id=".$row['user_id']."' class='user-link'>".$row['user_name']."</a></div></div>";
        	}
//         	加载更多
            if($cutotal_page!="1"){
                if($cupage==1){
                    echo "<div class='loadmore clearfix'>";
                    echo "<a href='search.php?page=2&cupage=1'>";
                    echo "<span>首页</span>";
                    echo "</a>";
                    echo "<a href='search.php?page=2&cupage=1'>";
                    echo "<span>上一页</span>";
                    echo "</a>";
                    echo "<a href='search.php?page=2&cupage=".($cupage+1)."'>";
                    echo "<span>下一页</span>";
                    echo "</a>";
                    echo "<a href='search.php?page=2&cupage=".$cutotal_page."'>";
                    echo "<span>尾页</span>";
                    echo "</a>";
                    echo "</div>";
                }else if($cupage==$cutotal_page){
                    echo "<div class='loadmore clearfix'>";
                    echo "<a href='search.php?page=2&cupage=1'>";
                    echo "<span>首页</span>";
                    echo "</a>";
                    echo "<a href='search.php?page=2&cupage=".($cupage-1)."'>";
                    echo "<span>上一页</span>";
                    echo "</a>";
                    echo "<a href='search.php?page=2&cupage=".$cutotal_page."'>";
                    echo "<span>下一页</span>";
                    echo "</a>";
                    echo "<a href='search.php?page=2&cupage=".$cutotal_page."'>";
                    echo "<span>尾页</span>";
                    echo "</a>";
                    echo "</div>";
                }else {
                    echo "<div class='loadmore clearfix'>";
                    echo "<a href='search.php?page=2&cupage=1'>";
                    echo "<span>首页</span>";
                    echo "</a>";
                    echo "<a href='search.php?page=2&cupage=".($cupage-1)."'>";
                    echo "<span>上一页</span>";
                    echo "</a>";
                    echo "<a href='search.php?page=2&cupage=".($cupage+1)."'>";
                    echo "<span>下一页</span>";
                    echo "</a>";
                    echo "<a href='search.php?page=2&cupage=".$cutotal_page."'>";
                    echo "<span>尾页</span>";
                    echo "</a>";
                    echo "</div>";
                }
            }
    	}else {
    	    echo "查无相关用户";
    	}
}
    	?>
    </div>

    <!--右边版心-->
    <div class="right-warp">
        <!--logo填充-->
        <div class="logo">
            <img src="../img/logo1.jpg" style="width: 260px; height: 260px">
        </div>
    </div>
</div>

</body>
<script type="text/javascript">
    function tipLogin(){
        confirm("请先登录")
    }

    // 点击上面筛选标签进行页面跳转
   if(<?php echo $_GET['page']?>=="2"){
    	$(".search-classify a").removeClass("choose-3")
        $("#nav-2").addClass("choose-3")
        $(".weibo-search").removeClass("choose-2")
        $(".user-search").removeClass("choose-2")
        $(".user-search").addClass("choose-2")
    }else{
    	$(".search-classify a").removeClass("choose-3")
        $("#nav-1").addClass("choose-3")
        $(".weibo-search").removeClass("choose-2")
        $(".user-search").removeClass("choose-2")
        $(".weibo-search").addClass("choose-2")
    }
</script>
</html>